package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.UserCoupon;
import com.example.mall.dto.UserCouponDto;
import com.example.mall.mapper.UserCouponMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserCouponQuery;
import com.example.mall.service.IUserCouponService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户优惠券表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

    @Autowired
    private UserCouponMapper userCouponMapper;

    @Override
    public PageDTO<UserCouponDto> queryPage(UserCouponQuery userCouponQuery) {
        if (userCouponQuery == null) {
            userCouponQuery = new UserCouponQuery();
        }
        Page<UserCoupon> page = userCouponQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserCoupon> cartPage = lambdaQuery()
                .like(userCouponQuery.getUserCouponId()!= null, UserCoupon::getUserCouponId, userCouponQuery.getUserCouponId())
                .like(userCouponQuery.getUserId()!= null, UserCoupon::getUserId, userCouponQuery.getUserId())
                .like(userCouponQuery.getOrderId()!= null, UserCoupon::getOrderId, userCouponQuery.getOrderId())
                .like(userCouponQuery.getTemplateId()!= null, UserCoupon::getTemplateId, userCouponQuery.getTemplateId())
                .like(userCouponQuery.getCouponCode()!= null, UserCoupon::getCouponCode, userCouponQuery.getCouponCode())
                .like(userCouponQuery.getStatus()!= null, UserCoupon::getStatus, userCouponQuery.getStatus())
                .like(userCouponQuery.getObtainType()!= null, UserCoupon::getObtainType, userCouponQuery.getObtainType())
                .apply(userCouponQuery.getObtainTime() != null,
                        "DATE_FORMAT(obtain_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userCouponQuery.getObtainTime())
                .apply(userCouponQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userCouponQuery.getStartTime())
                .apply(userCouponQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userCouponQuery.getEndTime())
                .apply(userCouponQuery.getUseTime() != null,
                        "DATE_FORMAT(use_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userCouponQuery.getUseTime())
                .orderByDesc(UserCoupon::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserCouponDto.class);
    }

    @Override
    public UserCoupon queryLoginById(Long userCouponId) {
        AssertUtil.isNotNull(userCouponId, "用户优惠券ID不能为空");
        return userCouponMapper.selectById(userCouponId);
    }

    @Override
    public List<UserCoupon> queryLoginList() {
        return this.list();
    }

    @Override
    public void insert(UserCouponDto userCouponDto) {
        UserCoupon userCoupon = new UserCoupon();
        CopyUtils.copyProperties(userCouponDto, userCoupon);
        userCoupon.setUserCouponId(CodeGenerateUtils.generateUniqueId());
        userCoupon.setStatus(BaseConstants.NOT_USED);
    }

    @Override
    public void update(UserCouponDto userCouponDto) {
        AssertUtil.isNotNull(userCouponDto.getUserCouponId(), "用户优惠券ID不能为空");
        UserCoupon userCoupon = userCouponMapper.selectById(userCouponDto.getUserId());
        AssertUtil.isNotNull(userCoupon, "用户优惠券不存在");
        CopyUtils.copyProperties(userCouponDto, userCoupon);
        userCoupon.setUpdateDate(new Date());
        userCouponMapper.updateById(userCoupon);
    }

    @Override
    public void delete(Long userCouponId) {
        AssertUtil.isNotNull(userCouponId, "用户优惠券ID不能为空");
        UserCoupon userCoupon = userCouponMapper.selectById(userCouponId);
        AssertUtil.isNotNull(userCoupon, "用户优惠券不存在");
        userCoupon.setStatus(BaseConstants.EXPIRED);
        userCoupon.setUpdateDate(new Date());
        userCouponMapper.updateById(userCoupon);
    }

    @Override
    public void deleteBatch(List<Long> userCouponIds) {
        AssertUtil.isNotNull(userCouponIds, "用户优惠券ID列表不能为空");
        List<UserCoupon> userCoupons = userCouponMapper.selectBatchIds(userCouponIds);
        AssertUtil.isNotNull(userCoupons, "用户优惠券不存在");
        for (UserCoupon userCoupon : userCoupons) {
            userCoupon.setStatus(BaseConstants.EXPIRED);
            userCoupon.setUpdateDate(new Date());
            userCouponMapper.updateById(userCoupon);
        }
    }
}
